package com.zhiyin.seal.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhiyin.seal.entity.bo.*;
import com.zhiyin.seal.entity.dto.ResponseDTO;
import com.zhiyin.seal.entity.dto.UserDTO;
import com.zhiyin.seal.entity.dto.WXEncryptDataDTO;
import com.zhiyin.seal.entity.vo.CompanyDepartmentVo;
import com.zhiyin.seal.entity.vo.CompanyVo;
import com.zhiyin.seal.entity.vo.UserDetailVo;
import com.zhiyin.seal.entity.vo.UserVo;
import com.zhiyin.seal.payment.WXPayUtil;
import com.zhiyin.seal.service.CompanyService;
import com.zhiyin.seal.service.SystemService;
import com.zhiyin.seal.service.UserService;
import com.zhiyin.seal.util.BeanUtil;
import com.zhiyin.seal.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zhiyin.seal.constant.SystemConstants.WX_GET_PHONE_NUMBER_URL;
import static com.zhiyin.seal.constant.SystemConstants.WX_LOGIN_URL;
import static com.zhiyin.seal.payment.WXPayConstants.*;

/**
 * 用户业务控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    CompanyService companyService;

    @Autowired
    SystemService systemService;

    @PostMapping("/register")
    public ResponseDTO<UserVo> register(@RequestBody UserDTO userDTO){
        UserBo userBo = BeanUtil.copy(userDTO,UserBo.class);
        userBo = userService.register(userBo);
        UserVo userVo = BeanUtil.copy(userBo,UserVo.class);

        return ResponseDTO.succData(userVo);
    }

    @GetMapping("/info")
    public ResponseDTO<UserVo> getUserInfo(@RequestParam String openId){
        UserBo userBo = userService.getUserInfo(openId);
        UserVo userVo = BeanUtil.copy(userBo,UserVo.class);

        return ResponseDTO.succData(userVo);
    }

    @GetMapping("/list/all")
    public ResponseDTO<List<UserVo>> getAllUser(@RequestParam String keywords){
        return ResponseDTO.succData(BeanUtil.copyList(userService.getAllUserList(keywords),UserVo.class));
    }

    @GetMapping("/detailinfo")
    public ResponseDTO<UserDetailVo> getUserDetailInfo(@RequestParam Long id){
        UserBo userBo = userService.getUserInfoById(id);
        UserDetailVo userDetailVo = BeanUtil.copy(userBo,UserDetailVo.class);
        CompanyBo companyBo = companyService.getUserDefaultCompany(userBo.getId());
        if(companyBo != null){
            userDetailVo.setDefaultCompany(BeanUtil.copy(companyBo, CompanyVo.class));

            CompanyUserBo companyUserBo = companyService.getLastCompanyUser(id,companyBo.getId());
            if(companyUserBo != null){
                userDetailVo.setUserAlias(companyUserBo.getUserAlias());
                CompanyDepartmentBo companyDepartmentBo = companyService.getDepartmentById(companyUserBo.getDepartmentId());
                if(companyDepartmentBo != null){
                    userDetailVo.setDepartment(BeanUtil.copy(companyDepartmentBo, CompanyDepartmentVo.class));
                }
            }
        }

        return ResponseDTO.succData(userDetailVo);
    }

    @PostMapping("/update")
    public ResponseDTO<Boolean> updateUser(@RequestBody UserDTO userDTO){
        UserBo userBo = BeanUtil.copy(userDTO,UserBo.class);
        Boolean result = userService.updateUserInfo(userBo);

        return ResponseDTO.succData(result);
    }

    @GetMapping("/infobyid")
    public ResponseDTO<UserVo> getUserInfoById(@RequestParam Long id){
        UserBo userBo = userService.getUserInfoById(id);
        UserVo userVo = BeanUtil.copy(userBo,UserVo.class);

        return ResponseDTO.succData(userVo);
    }

    @GetMapping("/info/by/company")
    public ResponseDTO<UserVo> getUserInfoByCompanyId(@RequestParam Long userId,@RequestParam Long companyId){
        CompanyUserBo companyUserBo = companyService.getLastCompanyUser(userId,companyId);
        if(companyUserBo != null){
            UserBo userBo = userService.getUserInfoById(userId);
            if(userBo != null){
                UserVo userVo = BeanUtil.copy(userBo,UserVo.class);
                userVo.setNickName(companyUserBo.getUserAlias());

                return ResponseDTO.succData(userVo);
            }

        }


        return ResponseDTO.succData(null);
    }

    @PostMapping("/sync")
    public ResponseDTO<Boolean> sync(@RequestBody UserDTO userDTO){
        UserBo userBo = BeanUtil.copy(userDTO,UserBo.class);
        return ResponseDTO.succData(userService.updateUserInfo(userBo));
    }

    @GetMapping("/wxlogin")
    public String wxLogin(@RequestParam String code){
        return HttpUtil.doGet(String.format(WX_LOGIN_URL,APP_ID,APP_SECRET,code));
    }

    @GetMapping("/get_phone")
    public ResponseDTO<String> getUserPhoneNumber(@RequestParam(required = true) String code){
        AccessTokenBo accessTokenBo = systemService.getAccessToken(1);
        if(accessTokenBo != null){
            String jsonString = "{\"code\":\"%s\"}";
            jsonString = String.format(jsonString,code);
            String result = HttpUtil.doPostJSON(String.format(WX_GET_PHONE_NUMBER_URL,accessTokenBo.getToken()),jsonString);
            if(result != null){
                JSONObject jsonObject = JSONObject.parseObject(result);
                JSONObject phoneObject = jsonObject.getJSONObject("phone_info");
                if(phoneObject != null){
                    String phoneNumber = phoneObject.getString("phoneNumber");

                    return ResponseDTO.succData(phoneNumber);
                } else {
                    return ResponseDTO.succMsg(result);
                }
            } else {
                return ResponseDTO.succMsg("接口调用失败");
            }
        }

        return ResponseDTO.succData(null);
    }

    @PostMapping(value = "/decode",produces = "application/json;charset=UTF-8")
    public String decode(@RequestBody WXEncryptDataDTO encryptData){
        return WXPayUtil.decodeData(encryptData.getEncryptedData(),encryptData.getSessionKey(),encryptData.getIv());
    }
}